package com.lankystar.logistucs.spring.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lankystar.logistucs.spring.dao.IOneOrderDao;
import com.lankystar.logistucs.spring.entity.Company;
import com.lankystar.logistucs.spring.entity.OneOrder;
import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.entity.UserType;

@Repository
public class OneOrderDaoImpl extends BaseDaoImpl implements IOneOrderDao{

	@Autowired
	private SessionFactory sf;
	
	public Session getSession(){
		System.out.println("session oneOrderDaoImpl");
		return sf.getCurrentSession();
	}

	@Override
	public Object[] selectOneOrderAndTwoOrder(int userId) {
		String sql = "from TwoOrder two left join two.user u join two.truck t where u.userId = "+userId+" and two.type = 1";
		Object[] ob = (Object[]) getSession().createQuery(sql).uniqueResult();
		return ob;
	}

	/**
	 * oneStatusKey=70 完成订单
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> selectOneOrderAndOneStatusByUserId(int userId) {
		String sql = "from TwoOrder two join two.user u join two.truck t join two.oneOrder one join one.oao oao join oao.oneStatus os where u.userId = "+userId+" and two.remove = "+0+" and two.type=1";
		List<Object[]> oblist = getSession().createQuery(sql).list();
		return oblist;
	}

	@Override
	public OneOrder selectOneOrderByTwoOrderId(int twoOrderId) {
		String hql = "select one from TwoOrder two join two.oneOrder one where two.twoOrderId = ?";
		OneOrder oneOrder = (OneOrder) getSession().createQuery(hql).setInteger(0, twoOrderId).uniqueResult();
		return oneOrder;
	}
	////////////////////////////////////////////////////////////////按条件查询订单
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> selectOrderAndStatus(String totalOrderNu, String pointOrderNu,int userId) {
		User user = (User) getSession().get(User.class, userId);
		UserType userType = user.getUserType();
		Company cp = user.getCompany();
		String hql = "";
		String hql_count = "";
		if(userType.getUserKey()==103){
			 hql += "from OneOrder one right join one.cargos c left join c.user u ";
			 hql_count += " where 1=1 and u.userId = "+userId+"";
			if(totalOrderNu!=null&&totalOrderNu!=""){
				hql_count +=" and c.totalOrderNu = '"+totalOrderNu+"' ";
			}
			if(pointOrderNu!=null&&pointOrderNu!=""){
				hql_count +=" and c.pointOrderNu = '"+pointOrderNu+"' ";
			}
		}
		if(userType.getUserKey()==102){
			hql += "from OneOrder one right join one.cargos c left join c.broker b ";
			hql_count += " where 1=1 and b.companyId = "+cp.getCompanyId()+"";
			if(totalOrderNu!=null&&totalOrderNu!=""){
				hql_count +=" and c.totalOrderNu = '"+totalOrderNu+"' ";
			}
			if(pointOrderNu!=null&&pointOrderNu!=""){
				hql_count +=" and c.pointOrderNu = '"+pointOrderNu+"' ";
			}
		}
		if(userType.getUserKey()==101){
			hql += "from OneOrder one right join one.cargos c left join c.fleet f ";
			hql_count += " where 1=1 f.companyId = "+cp.getCompanyId()+"";
			if(totalOrderNu!=null&&totalOrderNu!=""){
				hql_count +=" and c.totalOrderNu = '"+totalOrderNu+"' ";
			}
			if(pointOrderNu!=null&&pointOrderNu!=""){
				hql_count +=" and c.pointOrderNu = '"+pointOrderNu+"' ";
			}
		}
		
		List<Object[]> oneOrderlist = getSession().createQuery(hql+hql_count).list();
		return oneOrderlist;
	}
	
}
